package com.lyb.util;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.CallLog;
import android.text.TextUtils;

import com.lyb.app.App;
import com.lyb.ui.authentication.bean.CallLogBean;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Description:
 * Author     : Xiccc.
 * Date       : 2017/11/21 15:32
 */

public class CallLogUtil {

    /**
     * 读取通话记录
     */
    public static List<CallLogBean> getCallLog(Context context) {
        List<CallLogBean> mCallLogList = new ArrayList<>();
        ContentResolver cr = context.getContentResolver();
        // Uri uri = android.provider.CallLog.Calls.CONTENT_URI
        Uri uri = Uri.parse("content://call_log/calls");
        // 查询的列
        String[] projection = {CallLog.Calls.DATE, CallLog.Calls.NUMBER, CallLog.Calls.TYPE, CallLog.Calls
                .CACHED_NAME, CallLog.Calls.DURATION};
        Cursor cursor = null;
        try {
            cursor = cr.query(uri, projection, null, null, "date desc");

            if (cursor != null && cursor.getCount() > 0) {
                SimpleDateFormat sfd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                // 游标移动到第一项
                cursor.moveToFirst();
                for (int i = 0; i < cursor.getCount(); i++) {
                    cursor.moveToPosition(i);
                    String uid = "";
                    if (App.getConfig().getUserInfo() != null) {
                        uid = App.getConfig().getUserInfo().getUid() + "";
                    }
                    Date date = new Date(cursor.getLong(cursor.getColumnIndex(CallLog.Calls.DATE)));
                    String number = cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER));
                    int type = cursor.getInt(cursor.getColumnIndex(CallLog.Calls.TYPE));
                    // 缓存的名称与电话号码，如果它存在
                    String cachedName = cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_NAME));
                    long duration = cursor.getLong(cursor.getColumnIndex(CallLog.Calls.DURATION));

                    CallLogBean callLogBean = new CallLogBean();
                    callLogBean.setUserId(uid);
                    callLogBean.setNumber(number);
                    callLogBean.setName(TextUtils.isEmpty(cachedName) ? number : cachedName);
                    callLogBean.setType(type);
                    callLogBean.setDate(sfd.format(date));
                    callLogBean.setDuration(formatDuration(duration));
                    mCallLogList.add(callLogBean);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                if (cursor.isClosed()) {
                    cursor.close();
                }
            }
        }
        return mCallLogList;
    }

    /**
     * 格式化通话时长
     */
    private static String formatDuration(long duration) {
        StringBuilder sb = new StringBuilder();
        if (duration == 0) {
            sb.append("00:00");
        } else if (duration > 0 && duration < 60) {
            sb.append("00:");
            if (duration < 10) {
                sb.append("0");
            }
            sb.append(duration);

        } else if (duration > 60 && duration < 3600) {

            long min = duration / 60;
            long sec = duration % 60;
            if (min < 10) {
                sb.append("0");
            }
            sb.append(min);
            sb.append(":");

            if (sec < 10) {
                sb.append("0");
            }
            sb.append(sec);
        } else if (duration > 3600) {
            long hour = duration / 3600;
            long min = duration % 3600 / 60;
            long sec = duration % 3600 % 60;
            if (hour < 10) {
                sb.append("0");
            }
            sb.append(hour);
            sb.append(":");

            if (min < 10) {
                sb.append("0");
            }
            sb.append(min);
            sb.append(":");

            if (sec < 10) {
                sb.append("0");
            }
            sb.append(sec);
        }

        return sb.toString();
    }

}
